package gwt.test.EclipseMaven2Rpc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;



/**
 *  * Entry point classes define <code>onModuleLoad()</code>.
 *   */
public class App implements EntryPoint {

  /**
 *    * This is the entry point method.
 *       */
  public void onModuleLoad() {
    final Button button = new Button("Click me");
    final Label label = new Label();


    final ServiceAsync remoteService = (ServiceAsync) GWT.create(Service.class);
    
    ServiceDefTarget endpoint = (ServiceDefTarget) remoteService;
    String moduleRelativeURL = GWT.getModuleBaseURL() + "Service";
    endpoint.setServiceEntryPoint(moduleRelativeURL);

    final AsyncCallback callback = new AsyncCallback() {
	public void onSuccess(Object result) {
		label.setText("Answer from Server: '" + result.toString() + "'");
        }
        public void onFailure(Throwable caught) {
		label.setText("Server error: " + caught.getMessage());
	}
    };

    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
	remoteService.myMethode("Hi Server", callback);
      }
    });

    // Assume that the host HTML has elements defined whose
    // IDs are "slot1", "slot2".  In a real app, you probably would not want
    // to hard-code IDs.  Instead, you could, for example, search for all
    // elements with a particular CSS class and replace them with widgets.
    // 
    RootPanel.get("slot1").add(button);
    RootPanel.get("slot2").add(label);
  }
}
